package com.bcmusic.search.controller;

import com.bcmusic.music.feign.MusicFeign;
import com.bcmusic.music.pojo.Music;
import com.bcmusic.search.service.ESService;
import com.bcmusic.search.vo.CopyrightResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ESController  //类名
 * @Description : 提供各种搜索相关的接口  //描述
 * @Author : YG  //作者
 * @Date: 2020-10-26 10:03  //时间
 */
@RestController
@RequestMapping("/search")
@CrossOrigin(allowedHeaders = "*")
public class ESController {

    @Autowired
    private ESService esService;

    @Autowired
    private MusicFeign musicFeign;


    @GetMapping("/copyright")
    public Result<List<Map<String, Object>>>getCopyright(@RequestParam("musicName") String musicName,
                                                         @RequestParam("author") String author,
                                                         @RequestParam("user") String user,
                                                         @RequestParam("pageSize") Integer pageSize,
                                                         @RequestParam("pageNum") Integer pageNum) throws Exception{
        List<Map<String, Object>> copyright = esService.getCopyright(musicName, author, user, pageSize, pageNum);

        return new Result<>(true, StatusCode.OK,"版权信息查询成功",copyright);
    }

    @GetMapping("/music")
    public Result<List<Map<String, Object>>>getMusic(@RequestParam("musicName") String musicName,
                                                     @RequestParam("author")String author,
                                                     @RequestParam("pageSize")Integer pageSize,
                                                     @RequestParam("pageNum")Integer pageNum) throws Exception {

        List<Map<String, Object>> music = esService.getMusic(musicName,author,pageSize, pageNum);

        return new Result<>(true,StatusCode.OK,"音乐查询成功",music);
    }

    /**
     * 手动调用同步数据库先,测试
     * @return
     */
    @GetMapping("upload/copyright")
    public Result uploadCopyright()
    {
        List<CopyrightResult>list=new ArrayList<>();

        list.add(new CopyrightResult("七里香","周杰伦","qq音乐","国内"));
        list.add(new CopyrightResult("晴天","周杰伦","酷狗音乐","国内"));
        list.add(new CopyrightResult("海阔天空","beyond","qq音乐","国内"));
        list.add(new CopyrightResult("风再起时","张国荣","网易云音乐","国外"));
        esService.bulkSave("copyright",list);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 手动调用同步数据库先，测试
     * @return
     */
    @GetMapping("upload/music")
    public Result uploadmusic()
    {
        List<CopyrightResult>list=new ArrayList<>();
        List<Music> data = musicFeign.findAllMusic().getData();
        esService.bulkSave("music",data);
        return new Result(true,StatusCode.OK,"添加成功");
    }



}
